package com.carmanagement

import grails.transaction.Transactional

@Transactional
class ChartsDatasService {

    def getDatasAndColumnsForFullTankPriceTrend(Vehicle v) {
		return [datas:getDataForFullTankPriceTrend(v), cols :getFullTankPriceTrendColumns() ]
    }

    def getDatasAndColumnsForFullTankDistanceTrend(Vehicle v) {
		return [datas:getDataForFullTankDistanceTrend(v), cols :getFullTankDistanceTrendColumns() ]
    }

    def getDatasAndColumnsForFullTankPriceQuantities(Vehicle v){
		return [datas:getDataForFullTankPriceQuantities(v), cols :getFullTankPricePriceQuantities() ]
    }

    def getDatasAndColumnsForMaintenancePriceTrend(Vehicle v) {
		return [datas:getMaintenanceDataPriceTrend(v), cols :getMaintenancePriceTrendColumns() ]
    }

    private def getFullTankDistanceTrendColumns(){
		return [
		    ['string', 'number'],
		    ['number', 'Distance']
		]
    }

    private def getFullTankPricePriceQuantities(){
		return [
		    ['number', 'number'],
		    ['number', 'price , quant']
		]
    }

    private def getFullTankPriceTrendColumns(){
		return [
		    ['string', 'tata'],
		    ['number', 'price']
		]
    }

    private def getDataForFullTankDistanceTrend(Vehicle v){
		def result = []
		FullTank.findAllByVehicle(v).each {
		    def point = [
			it.date.format("yyyy-MM-dd"),
			it.mileage.round()
		    ]
		    result << point
		}
		return result
    }

    private def getDataForFullTankPriceTrend(Vehicle v){
		def result = []
		FullTank.findAllByVehicle(v).each {
		    def point = [
			it.date.format("yyyy-MM-dd"),
			it.cost.round()
		    ]
		    result << point
		}
		return result
    }

    private def getDataForFullTankPriceQuantities(Vehicle v){
		def result = []
		FullTank.findAllByVehicle(v).each {
		    def point = [
			it.cost.round(),
			it.quantity.round()
		    ]
		    result << point
		}
		return result
    }

    private def getMaintenanceDataPriceTrend(Vehicle v){
		def result = []
		Maintenance.findAllByVehicle(v).each {
		    if(it.cost){
			def point = [
			    it.date.format("yyyy-MM-dd"),
			    it.cost.round()
			]
			result << point
		    }
		}
		return result
    }

    private def getMaintenancePriceTrendColumns(){
		return [
		    ['string', 'Date'],
		    ['number', 'trends']
		]
    }
}
